import api from "../../../common/api";
var app = getApp();
Page({
  data: {
    id: '', //用户id（也即是被举报人id）
    img: '', //头像
    name: '', //名称
    address: '', //地址
    houseImg: '', //房源图片
    houseTitle: '', //房源标题
    room: '', //大小， 区， 室厅
    shouseId: '', //被举报合作房源id 
    // 定义笑脸数据
    faceList: [{
        id: 1,
        imgnoclick: '../images/tinified/badReview2.png',
        imgclick: '../images/tinified/badReview.png',
        value: '非常差',
      },
      {
        id: 2,
        imgnoclick: '../images/tinified/general2.png',
        imgclick: '../images/tinified/general.png',
        value: '一般',
      },
      {
        id: 3,
        imgnoclick: '../images/tinified/smilingFace2.png',
        imgclick: '../images/tinified/smilingFace.png',
        value: '超赞',
      }
    ],
    userRating: '',
    // -----------
    reportTypeList: [],
    reportType: '',
    textInfo: '', //input的内容
    height: 0,
    top: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getApp().globalData.loginPromise.then(() => {
      this.getNavHeight();
      this.setData({
        shouseId: options.cooperationId
      })
      this.getCooperationInfo(options)
      this.reporType()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // 拿到房源信息
  getCooperationInfo(options) {
    return new Promise((resolve, reject) => {
      let that = this
      let cooperationId = options.cooperationId
      api.getCooperationInfo({
        cooperationId
      }, (res) => {
        if (res && res.data && res.data.isSuccess) {
          let content = res.data.content
          let roomInfo = content.hmCooperationDelVO.room + '室' + content.hmCooperationDelVO.hall + '厅' +
            content.hmCooperationDelVO.toilet + '卫/' + content.hmCooperationDelVO.buildSize + '㎡/' + content.hmCooperationDelVO.area + content.hmCooperationDelVO.addr
          that.setData({
            id: content.jjrVO.bossEmployeeVO.userId, //被举报人id
            img: content.jjrVO.wxHeadImg, //头像
            address: content.jjrVO.bossEmployeeVO.storeAllName,
            name: content.jjrVO.bossEmployeeVO.realname, //名称
            houseImg: content.houImageVO.length!==0? content.houImageVO[0].url: '/images/defaultImgSmall.jpg', //房源图片
            houseTitle: content.hmCooperationDelVO.title, //房源标题
            room: roomInfo //大小， 区， 室厅
          })
          resolve();
        } else {
          reject();
        }
      })
    })
  },


  // 先拿到举报类型
  reporType() {
    return new Promise((resolve, reject) => {
      let that = this
      api.reportType({}, (res) => {
        if (res && res.data && res.data.isSuccess) {
          let tmpType = res.data.content
          let formatList = []
          for (let i in tmpType) {
            let tmp = {}
            tmp.key = i
            tmp.value = tmpType[i]
            formatList.push(tmp)
          }
          that.setData({
            reportTypeList: [...formatList]
          })
          resolve();
        } else {
          reject();
        }
      })
    })
  },
  // 选择房源问题类型
  chooseType(e) {
    let key = e.currentTarget.dataset.key
    this.setData({
      reportType: key,
    })
  },

  // 点击笑脸
  faceClick(e) {
    let id = e.currentTarget.dataset.id
    this.setData({
      userRating: id
    })
  },

  // 提交事件
  confirm() {
    return new Promise((resolve, reject) => {
      let that = this
      let {
        reportType, //反馈问题:1虚假房源2房源不可合作3其他问题
        id, //被举报人id
        shouseId, //被举报合作房源id
        userRating, //评分 1非常差 2一般3超赞
        textInfo //内容
      } = that.data
      api.addCooperationReport({
        reportType: reportType || null,
        content: textInfo || null,
        beReportId: id,
        cooperationId: shouseId,
        userRating
      }, (res) => {
        if (res && res.data && res.data.isSuccess) {
          wx.showToast({
            title: '评价成功，谢谢合作！',
            icon: "none",
            duration: 1000,
          })
          that.setData({
            reportType: '',
            userRating: '',
            textInfo: ''
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            })
          }, 500)
          resolve();
        } else {
          reject();
        }
      })
    })
  },

  // 拿到输入框的数据
  bindTextAreaBlur(e) {
    this.setData({
      textInfo: e.detail.value
    })
  },

  // 设置导航栏的高度
  getNavHeight() {
    this.setData({
      height: app.globalData.navHeight,
      top: app.globalData.navTop,
    })
  },
  // 点击返回按钮
  goToBack() {
    wx.showModal({
      title: '温馨提示',
      cancelText: '立即退出',
      confirmText: '继续评价',
      content: '您还未对本次咨询进行评价，请确认是否退出？',
      cancelColor:'#ccc',
      confirmColor: '#F75352',
      success(res) {
        if (res.confirm) {
          
        } else if (res.cancel) {
          wx.navigateBack({
            delta: 1,
          })
        }
      }
    })
  }
})